package com.ywd.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.platform.dto.DeviceDto;
import com.ywd.platform.entity.DeviceEntity;
import com.ywd.platform.entity.VehicleEntity;
import com.ywd.platform.mapper.DeviceMapper;
import com.ywd.platform.netty.jt080.po.AuthBody;
import com.ywd.platform.page.DevicePageDto;
import com.ywd.platform.exception.RPage;
import com.ywd.platform.vo.DeviceVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * YWD
 * 2025/8/17
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, DeviceEntity> {

    public RPage<List<DeviceVo>> getDevicePage(DevicePageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<DeviceVo> list = this.baseMapper.getDevicePage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public DeviceVo getDeviceDetails(Integer id) {
        return this.baseMapper.getDeviceDetails(id);
    }

    public Boolean saveDevice(DeviceDto dto) {
        return this.save(BeanUtil.copyProperties(dto, DeviceEntity.class));
    }

    public Boolean updateDevice(DeviceDto dto) {
        return this.updateById(BeanUtil.copyProperties(dto, DeviceEntity.class));
    }

    public Boolean deleteDevice(Integer id) {
        return this.removeById(id);
    }

    //===================================================================================
    public DeviceVo getDeviceBySimCard(String terminalPhone) {
        LambdaQueryWrapper<DeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(DeviceEntity::getIsDeleted, false)
                .eq(DeviceEntity::getSimCard, terminalPhone);
        return BeanUtil.copyProperties(this.getOne(queryWrapper), DeviceVo.class);
    }

    public Boolean updateOnlineStatus(String terminalPhone) {
        LambdaUpdateWrapper<DeviceEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(DeviceEntity::getSimCard, terminalPhone)
                .set(DeviceEntity::getOnlineStatus, true)
                .set(DeviceEntity::getLastOnlineTime, LocalDateTime.now().toString());
        return this.update(updateWrapper);
    }

    public Boolean updatePart(String terminalPhone, AuthBody body) {
        LambdaUpdateWrapper<DeviceEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(DeviceEntity::getSimCard, terminalPhone)
                .set(DeviceEntity::getImei, body.getImei())
                .set(DeviceEntity::getFirmwareVersion, body.getSoftwareVersion())
                .set(DeviceEntity::getOnlineStatus, true)
                .set(DeviceEntity::getLastOnlineTime, LocalDateTime.now().toString());
        return this.update(updateWrapper);
    }

    //=================================================================================
    public List<Long> getListToDeviceScheduler() {
        LambdaQueryWrapper<DeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(DeviceEntity::getIsDeleted, false)
                .eq(DeviceEntity::getOnlineStatus, true);
        List<DeviceEntity> list = this.list(queryWrapper);
        return list.stream().map(DeviceEntity::getId).toList();
    }

    public Boolean updateOnlineStatusById(Long deviceId) {
        LambdaUpdateWrapper<DeviceEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(DeviceEntity::getId, deviceId)
                .set(DeviceEntity::getOnlineStatus, false);
        return this.update(updateWrapper);

    }
}
